// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ui;

import java.util.Stack;

public final class TextBox extends UIElement {
    public boolean ResizeParent;
    UIFont font;
    Stack<String> Lines = new Stack();
    String Text;
    public boolean Centred = false;

    public TextBox(UIFont _font, int x, int y, int width, String text) {
        this.font = _font;
        this.x = (double)x;
        this.y = (double)y;
        this.Text = text;
        this.width = (float)width;
        this.Paginate();
    }

    @Override
    public void onresize() {
        this.Paginate();
    }

    @Override
    public void render() {
        if (this.isVisible()) {
            super.render();
            this.Paginate();
            int _int = 0;

            for (String string : this.Lines) {
                if (this.Centred) {
                    TextManager.instance
                        .DrawStringCentre(
                            this.font,
                            (double)this.getAbsoluteX().intValue() + this.getWidth() / 2.0,
                            (double)(this.getAbsoluteY().intValue() + _int),
                            string,
                            1.0,
                            1.0,
                            1.0,
                            1.0
                        );
                } else {
                    TextManager.instance
                        .DrawString(
                            this.font, (double)this.getAbsoluteX().intValue(), (double)(this.getAbsoluteY().intValue() + _int), string, 1.0, 1.0, 1.0, 1.0
                        );
                }

                _int += TextManager.instance.MeasureStringY(this.font, (String)this.Lines.get(0));
            }

            this.setHeight((double)_int);
        }
    }

    @Override
    public void update() {
        this.Paginate();
        int _int = 0;

        for (String string : this.Lines) {
            _int += TextManager.instance.MeasureStringY(this.font, (String)this.Lines.get(0));
        }

        this.setHeight((double)_int);
    }

    private void Paginate() {
        int int0 = 0;
        this.Lines.clear();
        String[] string0 = this.Text.split("<br>");

        for (String string1 : string0) {
            if (string1.length() == 0) {
                this.Lines.add(" ");
            } else {
                while (true) {
                    int int1 = string1.indexOf(" ", int0 + 1);
                    int int2 = int1;
                    if (int1 == -1) {
                        int2 = string1.length();
                    }

                    int int3 = TextManager.instance.MeasureStringX(this.font, string1.substring(0, int2));
                    if ((double)int3 >= this.getWidth()) {
                        String string2 = string1.substring(0, int0);
                        string1 = string1.substring(int0 + 1);
                        this.Lines.add(string2);
                        int1 = 0;
                    } else if (int1 == -1) {
                        this.Lines.add(string1);
                        break;
                    }

                    int0 = int1;
                    if (string1.length() <= 0) {
                        break;
                    }
                }
            }
        }
    }

    public void SetText(String text) {
        this.Text = text;
        this.Paginate();
    }
}
